package rate.mock

import java.io.{File, PrintWriter}
import java.text.SimpleDateFormat

import rate.bean.Rate
import com.google.gson.Gson

import scala.collection.mutable.ArrayBuffer
import scala.util.Random

/**
 * 用户购物数据模拟程序
 */
object Simulator {
    //模拟数据
    //用户ID
    val arr1: ArrayBuffer[Int] = ArrayBuffer[Int]()
    for (i <- 1 to 50) {
        arr1 += i
    }
    val arr2: ArrayBuffer[Int] = ArrayBuffer[Int]()
    for (i <- 1 to 150) {
        arr2 += i
    }

    def genRate(): Rate = {
        //随机物品ID
        val itemIDRandom = arr2(Random.nextInt(arr2.length))

        //随机物品评分
        val scoreRandom = Random.nextInt(6)

        //随机用户ID
        val userID = arr1(Random.nextInt(arr1.length))

        Rate(userID, item_id = itemIDRandom, scoreRandom)
    }

    //测试模拟数据
    def main(args: Array[String]): Unit = {
        val printWriter = new PrintWriter(new File("rate_info.json"))
        val gson = new Gson()
        for (i <- 1 to 2000) {
            println(s"第{$i}条")
            val jsonString = gson.toJson(genRate())
            println(jsonString)
            //{"user_id":"用户ID_14","item_id":"物品ID_104","score":2.0}
            printWriter.write(jsonString + "\n")
        }
        printWriter.flush()
        printWriter.close()
    }
}